/*
 * Copyright 2012 Oracle and/or its affiliates.
 * All rights reserved.  You may not modify, use,
 * reproduce, or distribute this software except in
 * compliance with  the terms of the License at:
 * http://developers.sun.com/license/berkeley_license.html
 */


/*
 * Copyright 2009 Sun Microsystems, Inc.
 * All rights reserved.  You may not modify, use,
 * reproduce, or distribute this software except in
 * compliance with  the terms of the License at:
 * http://developer.sun.com/berkeley_license.html
 */
package cart.ejb;

import java.util.List;
import javax.ejb.EJB;
import static java.lang.System.out;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.*;

@Stateless
@Remote
//@DeclareRoles("users")
@RolesAllowed("users")
public class CartBean implements Cart {
    List<String> contents;
    int customerId;
    String customerName;
    
    @EJB
    App app;
    @Resource SessionContext ctx;
    @PostConstruct
    void init(){
        customerId = app.createdCart();
        out.println("CartBean "+customerId+" init");
    }
    
    @PreDestroy
    void onDestroy(){
        app.removeCart();
        out.println("CartBean "+customerId+" destroy");
    }
//    
//    @PrePassivate
//    void onPassivate(){
//        out.println("Cart Id="+customerId+" passivate.");
//    }
//    
//    @PostActivate
//    void onActive(){
//        out.println("Cart Id="+customerId+" active.");
//    }


    @Override
    public int getId() {
        return this.customerId;
    }

    @Override
    public String getName() {
        return customerName;
    }

    //@RolesAllowed("users")
    @Override
    public void setName(String name) {
  
        boolean s = ctx.isCallerInRole("users");
        
        customerName = name + " "+s +"{" + ctx.getCallerPrincipal().getName()+"}";
        //customerName = name;
//        try {
//            Thread.sleep(200000);
//        } catch (InterruptedException ex) {
//            Logger.getLogger(CartBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }
}
